﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScrapySharp.Extensions;
using V5_Apps.Plugins.Qvod._Class.Model;
namespace V5_Apps.Plugins.Qvod._Class {
    public class QvodHelper {
        private static readonly QvodHelper m_Instance = new QvodHelper();
        /// <summary>
        /// 
        /// </summary>
        public static QvodHelper Instance {
            get { return m_Instance; }
        }

        public string ToUrl(ModelSpiderModule SpiderModel, string HtmlContent, ref string encode, string CssUrl, bool cssIsReg) {
            string qvodUrl = string.Empty, tempQvodUrl = string.Empty;
            if (string.IsNullOrEmpty(HtmlContent)) {
                HtmlContent = HttpHelper.Instance.GetHttpPage(CssUrl, ref encode, 60000);
            }
            //
            string cssUrl = CssUrl;
            string cssHtmlContent = HtmlContent;
            string cssSelect = SpiderModel.CssSelect;
            var cssAttr = SpiderModel.CssAttr;
            var cssInclude = SpiderModel.CssInclude;
            var cssIsHttp = SpiderModel.CssIsHttp;
            //
            var htmlDocument = new HtmlAgilityPack.HtmlDocument();
            htmlDocument.LoadHtml(cssHtmlContent);
            var css = htmlDocument.DocumentNode;
            var list = css.CssSelect(cssSelect);
            foreach (var cList in list) {
                if (!string.IsNullOrEmpty(cssAttr)) {
                    var valueObj = cList.Attributes[cssAttr];
                    if (valueObj != null) {
                        var value = valueObj.Value.ToString();
                        tempQvodUrl = string.IsNullOrEmpty(value) ? string.Empty : Bind_GetResult(value, cssInclude, cssIsHttp, cssUrl, encode, cssIsReg);
                        if (!string.IsNullOrEmpty(tempQvodUrl)) {
                            qvodUrl += tempQvodUrl + "||||";
                            continue;
                        }
                    }
                }
                else {
                    var valueObj = cList.OuterHtml;
                    if (!string.IsNullOrEmpty(valueObj)) {
                        var value = valueObj.ToString();
                        tempQvodUrl = string.IsNullOrEmpty(value) ? string.Empty : Bind_GetResult(value, cssInclude, cssIsHttp, cssUrl, encode, cssIsReg);
                        if (!string.IsNullOrEmpty(tempQvodUrl)) {
                            qvodUrl += tempQvodUrl + "||||";
                            continue;
                        }
                    }
                }
            }
            if (!string.IsNullOrEmpty(qvodUrl)) {
                qvodUrl = qvodUrl.Remove(qvodUrl.Length - 4, 4);
            }
            return qvodUrl;
        }

        private string Bind_GetResult(string value, string cssInclude, bool cssIsHttp, string cssUrl, string encode, bool isReg) {
            if (ContentInclude(value, cssInclude)) {
                if (cssIsHttp) {
                    string qUrl = HttpHelper.Instance.FormatUrl(cssUrl, value);
                    string htmlContent = HttpHelper.Instance.GetHttpPage(qUrl, ref encode, 60000);
                    string qvodList = isReg ? HttpHelper.Instance.getQvodUrls(htmlContent) : htmlContent;
                    if (!string.IsNullOrEmpty(qvodList)) {
                        return qvodList;
                    }
                }
                else {
                    value = HttpUtility.UrlDecode(value);
                    string qvodList = isReg ? HttpHelper.Instance.getQvodUrls(value) : value;
                    if (!string.IsNullOrEmpty(qvodList)) {
                        return qvodList;
                    }
                }
            }
            return string.Empty;
        }

        //包含
        private bool ContentInclude(string htmlContent, string includes) {
            if (string.IsNullOrEmpty(includes)) {
                return true;
            }
            bool IsInclude = false;
            string[] arrInclude = includes.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in arrInclude) {
                if (htmlContent.IndexOf(s.ToString()) > -1) {
                    IsInclude = true;
                }
            }
            return IsInclude;
        }
        //不包含
        private bool ContentOutclude(string htmlContent, string includes) {
            if (string.IsNullOrEmpty(includes)) {
                return true;
            }
            bool IsNoInclude = false;
            string[] arrInclude = includes.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string item in arrInclude) {
                if (htmlContent.IndexOf(item.ToString()) > -1) {
                    IsNoInclude = true;
                    break;
                }
            }
            return IsNoInclude;
        }


    }
}